package com.leon.concurrency;

import java.util.LinkedList;

public class LeonQueue<E> {

    private static final int MAX_CAPACITY = 1000;

    private LinkedList<E> data = new LinkedList<>();

    private int size;


    public E poll() {
        E e = data.poll();
        return e;
    }

    public synchronized void offer(E e) {

        try {
            if (size == MAX_CAPACITY) {
                wait();
                data.addLast(e);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }


        if (size > 0) {
            notifyAll();
        }
    }

    public synchronized E take() {
        E e = null;

        try {
            if (size < 1) {
                wait();
            }
            e = data.peek();
            notifyAll();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        return e;

    }


}
